package com.exam.controller.exam;

import com.exam.common.Result;
import com.exam.common.SuperController;
import com.exam.pojo.model.UserModel;
import com.exam.pojo.param.UserParam;
import com.exam.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (User)表控制层
 * @Author ChengKe
 * @Date  2023-01-23
 */
@Api(tags = "User")
@RestController
@RequestMapping("/user")
public class UserController extends SuperController {

    @Resource
    private UserService userService;


    @ApiOperation("分页查询用户信息")
    @PostMapping("/{pageNum}/{pageSize}")
    // @PreAuthorize("hasRole('Admin')")
    public Result queryByPage(@RequestBody UserParam user,
                              @PathVariable("pageNum") Integer pageNum,
                              @PathVariable("pageSize") Integer pageSize) {
        return success(this.userService.queryByPage(user, pageNum, pageSize));
    }

    @ApiOperation("通过主键查询用户")
    @GetMapping("/{id}")
    public Result queryById(@PathVariable("id") String id) {
        return success(this.userService.queryById(id));
    }


    @ApiOperation("管理员新增用户")
    @PostMapping
    // @PreAuthorize("hasRole('Admin')")
    public Result add(@RequestBody UserParam user) {
        return this.userService.insert(user);
    }

    @ApiOperation("当前用户修改个人信息")
    @PatchMapping("/patch")
    public Result patchInfo(@RequestBody UserParam user){
        userService.patchInfo(user);
        return Result.ok("编辑成功");
    }

    @ApiOperation("获取当前用户信息")
    @GetMapping("/info")
    public Result getUserInfo(){
        UserModel userInfo = userService.getUserInfo();
        return Result.ok(userInfo);
    }


    @ApiOperation("管理员修改用户")
    @PutMapping
    // @PreAuthorize("hasRole('Admin')")
    public Result edit(@RequestBody UserParam user) {
        return success(this.userService.update(user));
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    // @PreAuthorize("hasRole('Admin')")
    public Result deleteById(@PathVariable("id") String id) {
        return success(this.userService.deleteById(id));
    }


    @ApiOperation("注册用户")
    @PostMapping("/registeredUser")
    public Result registeredUser(@RequestBody UserParam user) {
        return this.userService.registeredUser(user);
    }

    @ApiOperation("修改密码")
    @PostMapping("/password")
    public Result updatePassword(@RequestBody UserParam user){
        return this.userService.updatePassword(user);
    }
}

